//
//  SinpleCode.m
//  师傅午分享
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "SinpleCode.h"
#import <objc/runtime.h>

@implementation SinpleCode

+ (void)simpCode:(Class)cls {
    // 拼接字符串
    NSMutableString *initHeadStr = [@"- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {\n\tif (self = [super init]) {" mutableCopy];
    NSString *initTailStr = @"\n\t}\n\treturn self;\n}";
    // 获取类中属性名
    unsigned int count = 0;
    // 把类传进去返回的是一个属性的数组
    objc_property_t *propertys = class_copyPropertyList([cls class], &count);
       for (int i = 0; i < count; i++) {
        objc_property_t property = propertys[i];
        const char *propertyStr = property_getName(property);
           NSString *propertydStr = [NSString stringWithFormat:@"\t\t_%s = [aDecoder decodeObjectForKey:@\"%s", propertyStr, propertyStr];
           [initHeadStr appendString:propertydStr];
           NSLog(@"%@", propertydStr);
    }
    [initHeadStr appendString:initTailStr];
//    NSLog(@"%s", propertyStr);
    NSLog(@"%@", initHeadStr);
    // 在os line 中才能获取 桌面路径
    NSString *deskPath = [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject];
    NSString *file = [deskPath stringByAppendingPathComponent:@"SimpleFile"];
    NSData *data = [initHeadStr dataUsingEncoding:NSUTF8StringEncoding];
    if ([data writeToFile:file atomically:YES]) {
        NSLog(@"创建成功");
    } else {
        NSLog(@"创建失败");
    }
}




@end
